﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_Views_module_ManagerReview : System.Web.UI.UserControl
{
    reviewBUS reBus;
    CustomerBus cusBus;
    productBus proBus;
    protected void Page_Load(object sender, EventArgs e)
    {
        reBus = new reviewBUS();
        cusBus = new CustomerBus();
        proBus = new productBus();
        if (!IsPostBack) 
        {
            LoadData();
        }
    }
    public void LoadData() 
    {
        DataTable table = tableDefault();
        DataTable dt = reBus.getAllReview();
        DataRow dr;
        foreach (DataRow r in dt.Rows)
        {
           Customer cus = cusBus.getCustomerById(Convert.ToInt32(r["customer_id"]));
           product pro = proBus.getProducstByID(Convert.ToInt32(r["product_id"]));
            dr = table.NewRow();
            dr["review_id"] = r["review_id"];
            dr["product_id"] = r["product_id"];
            dr["customer_id"] = r["customer_id"];
            dr["product_name"] = pro.Product_name;
            dr["customer_name"] = cus.First_name;
            dr["subject"] = r["subject"];
            dr["comment"] = r["comment"];
            dr["status"] = r["status"];
            table.Rows.Add(dr);
        }
        dtList.DataSource = table;
        dtList.DataBind();


    }
    public DataTable tableDefault() 
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("review_id");
        dt.Columns.Add("product_id");
        dt.Columns.Add("customer_id");
        dt.Columns.Add("product_name");
        dt.Columns.Add("customer_name");
        dt.Columns.Add("subject");
        dt.Columns.Add("comment");
        dt.Columns.Add("status");
        return dt;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        dtList.EditItemIndex = -1;
        LoadData();
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
    }
    protected void dtList_EditCommand(object source, DataListCommandEventArgs e)
    {
        int n = (int)e.Item.ItemIndex;
        dtList.EditItemIndex = n;
        LoadData();
    }
    protected void dtList_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        review re = new review();
        re.Review_id = Convert.ToInt32(dtList.DataKeys[e.Item.ItemIndex]);
        re.Customer_id = int.Parse(((Label)e.Item.FindControl("lbCustomerId")).Text.ToString());
        re.Product_id = int.Parse(((Label)e.Item.FindControl("lbProductId")).Text.ToString());
        re.Subject = ((TextBox)e.Item.FindControl("txtSubject")).Text.ToString();
        re.Comment = ((TextBox)e.Item.FindControl("txtComment")).Text.ToString();
        re.Status = Convert.ToBoolean(((TextBox)e.Item.FindControl("txtStatus")).Text.ToString());
        if (reBus.UpdateReview(re))
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "Test", "<script>alert('Sửa thành công')</script>", false);
            dtList.EditItemIndex = -1;
            LoadData();
        }
        else 
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "Test", "<script>alert('Sửa không thành công')</script>", false);
        }
    }
    protected void dtList_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int n = Convert.ToInt32(dtList.DataKeys[e.Item.ItemIndex]);
        if (reBus.deleteReview(n))
        {
            this.LoadData();
        }
        else 
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "Test", "<script>alert('Xóa không thành công')</script>", false);
        }
    }
}